package com.m7.imkfsdk;

import android.content.Context;
import android.content.SharedPreferences;

import com.moor.imkf.gson.Gson;
import com.moor.imkf.gson.GsonBuilder;

import java.text.SimpleDateFormat;

/**
 * 保存用户信息工具类
 */
public class SharedPreferencesUtil {

    /**
     * 保存刷新角色信息(refresh_token接口返回数据)
     */
    public static void saveRoleInfo(Context context, String data) {
        SharedPreferences userInfo = context.getSharedPreferences("roleInfo", context.MODE_PRIVATE);
        SharedPreferences.Editor editor = userInfo.edit();
        editor.putString("RoleInfo", data);
        editor.commit();
    }


    /**
     * 获取角色信息(refresh_token接口返回数据)
     */
    public static RefreshtokenEntity.BodyBean getRoleInfo(Context context) {
        SharedPreferences role = context.getSharedPreferences("roleInfo", context.MODE_PRIVATE);
        String roleInfo = role.getString("RoleInfo", null);
        if (role != null) {
            Gson gson = new GsonBuilder().create();
            RefreshtokenEntity rte = gson.fromJson(roleInfo, RefreshtokenEntity.class);
            return rte.getBody();
        } else {
            return null;
        }

    }

    /**
     * 保存用户信息到本地
     */
    public static void saveUserInfo(Context context, String data) {
        SharedPreferences userInfo = context.getSharedPreferences("userInfo", context.MODE_PRIVATE);
        SharedPreferences.Editor editor = userInfo.edit();
        editor.putString("UserInfo", data);
        editor.commit();
    }

    /**
     * 保存房源
     */
    public static void saveHomeRes(Context context, long time, String data) {
        SharedPreferences homeRes = context.getSharedPreferences("homeRes", context.MODE_PRIVATE);
        SharedPreferences.Editor editor = homeRes.edit();
        editor.putLong("time", time);
        editor.putString("data", data);
        editor.commit();
    }


    /**
     * 从本地获取房源
     */
    public static String getHomeRes(Context context) {
        SharedPreferences homeRes = context.getSharedPreferences("homeRes", context.MODE_PRIVATE);
        String data = homeRes.getString("data", null);
        return data;
    }

    /**
     * 从本地获取房源保存时间是否过期
     */
    public static boolean getHomeResTimeIsOver(Context context) {
        SharedPreferences homeRes = context.getSharedPreferences("homeRes", context.MODE_PRIVATE);
        Long data = homeRes.getLong("time", 0);

        if (System.currentTimeMillis() - data > 24 * 60 * 60 * 1000) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 从本地获取UserId
     */
    public static LoginEntity getUserInfo(Context context) {
        SharedPreferences user = context.getSharedPreferences("userInfo", context.MODE_PRIVATE);
        String userInfo = user.getString("UserInfo", null);
        if (user != null) {
            Gson gson = new GsonBuilder().create();
            return gson.fromJson(userInfo, LoginEntity.class);
        } else {
            return null;
        }

    }

    /**
     * 保存token
     */
    public static void saveToken(Context context, String token) {
        SharedPreferences userInfo = context.getSharedPreferences("token", context.MODE_PRIVATE);
        SharedPreferences.Editor editor = userInfo.edit();
        editor.putString("Token", token);
        editor.commit();
    }

    /**
     * 获取token
     */
    public static String getToken(Context context) {
        SharedPreferences user = context.getSharedPreferences("token", context.MODE_PRIVATE);


        return user.getString("Token", "");
    }


    /**
     * 保存用户账户信息到本地
     */
    public static void saveUserAccount(Context context, String data) {
        SharedPreferences accountInfo = context.getSharedPreferences("userAccount", context.MODE_PRIVATE);
        SharedPreferences.Editor editor = accountInfo.edit();
        editor.putString("UserAccount", data);
        editor.commit();
    }

    /**
     * 保存店铺信息到本地
     */
    public static void saveStoreInfo(Context context, String data) {
        SharedPreferences userInfo = context.getSharedPreferences("storeInfo", context.MODE_PRIVATE);
        SharedPreferences.Editor editor = userInfo.edit();
        editor.putString("StoreInfo", data);
        editor.commit();
    }

    /**
     * 保存用户信息到本地
     */
    public static void saveData(Context context, String preferName, String dataName, String data) {
        SharedPreferences dataInfo = context.getSharedPreferences(preferName, context.MODE_PRIVATE);
        SharedPreferences.Editor editor = dataInfo.edit();
        editor.putString(dataName, data);
        editor.commit();
    }

    /**
     * 保存支付信息到本地
     */
    public static void saveWeOrderNum(Context context, String orderNum) {
        SharedPreferences payInfo = context.getSharedPreferences("orderNum", context.MODE_PRIVATE);
        SharedPreferences.Editor editor = payInfo.edit();
        editor.putString("orderNum", orderNum);
        editor.commit();
    }/*
     * 保存支付信息到本地
     */

    public static String getWeOrderNum(Context context) {
        SharedPreferences user = context.getSharedPreferences("orderNum", context.MODE_PRIVATE);
        String orderNum = user.getString("orderNum", "default");

        return orderNum;

    }

    /**
     * 保存支付信息到本地
     */
    public static void saveWeChatPay(Context context, String name, String type, float money) {
        SharedPreferences payInfo = context.getSharedPreferences(name, context.MODE_PRIVATE);
        SharedPreferences.Editor editor = payInfo.edit();
        editor.putString("PayType", type);
        editor.putFloat("PayMoney", money);
        editor.commit();
    }


    /**
     * 保存支付信息到本地
     */
    public static void saveWechatPayType(Context context, String type) {
        SharedPreferences payInfo = context.getSharedPreferences("WechatPaytg", context.MODE_PRIVATE);
        SharedPreferences.Editor editor = payInfo.edit();
        editor.putString("payType", type);
        editor.commit();
    }


    public static String getWechatPayType(Context context) {
        SharedPreferences user = context.getSharedPreferences("WechatPaytg", context.MODE_PRIVATE);
        String payValue = user.getString("payType", "0");
        return payValue;
    }

    public static void saveWechatStatus(Context context, boolean status) {
        SharedPreferences payInfo = context.getSharedPreferences("orderNum", context.MODE_PRIVATE);
        SharedPreferences.Editor editor = payInfo.edit();
        editor.putBoolean("status", status);
        editor.commit();
    }

    public static boolean getWechatStatus(Context context) {
        SharedPreferences user = context.getSharedPreferences("orderNum", context.MODE_PRIVATE);
        boolean statusValue = user.getBoolean("status", false);
        return statusValue;
    }


    /**
     * 保存支付信息到本地 订单
     */
    public static void saveWechatPayTypeForOrder(Context context, String type) {
        SharedPreferences payInfo = context.getSharedPreferences("WechatPaytgOrder", context.MODE_PRIVATE);
        SharedPreferences.Editor editor = payInfo.edit();
        editor.putString("payTypeOrder", type);
        editor.commit();
    }

    //订单
    public static String getWechatPayTypeForOrder(Context context) {
        SharedPreferences user = context.getSharedPreferences("WechatPaytgOrder", context.MODE_PRIVATE);
        String payValue = user.getString("payTypeOrder", "0");
        return payValue;
    }

    public static void saveWechatStatusForOrder(Context context, boolean status) {
        SharedPreferences payInfo = context.getSharedPreferences("orderNumForOrder", context.MODE_PRIVATE);
        SharedPreferences.Editor editor = payInfo.edit();
        editor.putBoolean("statusForOrder", status);
        editor.commit();
    }


    public static boolean getWechatStatusForOrder(Context context) {
        SharedPreferences user = context.getSharedPreferences("orderNumForOrder", context.MODE_PRIVATE);
        boolean statusValue = user.getBoolean("statusForOrder", false);
        return statusValue;
    }


    /**
     * 保存支付信息到本地
     *
     * @param context 上下文对象
     * @param name    SharedPreferences名字
     */

    public static void saveWeChatPay(Context context, String name, String type, String money, boolean isCooper) {
        SharedPreferences payInfo = context.getSharedPreferences(name, context.MODE_PRIVATE);
        SharedPreferences.Editor editor = payInfo.edit();
        editor.putString("PayType", type);
        editor.putString("PayMoney", money);
        editor.putBoolean("isCooper", isCooper);
        editor.commit();
    }
    /**
     * 修改用户数据
     *//*
    public static void SubmitUserInfo(Context context, String name, String data, OkHttpUtil.OnDataListener dataListener){
        Gson gson = new GsonBuilder().create();
        String jsonString = gson.toJson(new UpUserBasicInfo(getUserId(context), name, data));
        OkHttpUtil.postJson(Constant.URL.UpdateUserBasicInformation, DesUtil.encrypt(jsonString), dataListener);
    }

    *//**
     * 修改店铺数据
     *//*
    public static void SubmitStoreInfo(Context context, String mallId, String name, String data, OkHttpUtil.OnDataListener dataListener){
        Gson gson = new GsonBuilder().create();
        String jsonString = gson.toJson(new UpMallBasicInfo(getUserId(context), mallId, name, data));
        OkHttpUtil.postJson(Constant.URL.UpdateMallBasicInformation, DesUtil.encrypt(jsonString), dataListener);
    }*/



    /**
     * 保存上一次邀请好友成为VIP的时间(请求时间+有效时间)
     */
    public static void saveLastInvitFriendTime(Context context, String data) {
        SharedPreferences userInfo = context.getSharedPreferences("lastInvitFried", context.MODE_PRIVATE);
        SharedPreferences.Editor editor = userInfo.edit();
        editor.putString("lastTime", data);
        editor.commit();
    }


    /**
     * 获取上一次邀请好友成为VIP的时间(请求时间+有效时间)
     */
    public static String getLastInvitFriendTime(Context context) {
        SharedPreferences role = context.getSharedPreferences("lastInvitFried", context.MODE_PRIVATE);
        return role.getString("lastTime", "0");
    }


    /**
     * 保存上一次首页广告的时间(请求时间+有效时间)
     */
    public static void saveLastHome2vAdvertTime(Context context, String data) {
        SharedPreferences userInfo = context.getSharedPreferences("lasthomev2advert", context.MODE_PRIVATE);
        SharedPreferences.Editor editor = userInfo.edit();
        editor.putString("lasthome2vAdvertTime", data);
        editor.commit();
    }


    /**
     * 获取上一次首页广告的时间(请求时间+有效时间)
     */
    public static String getLastHome2vAdvertTime(Context context) {
        SharedPreferences role = context.getSharedPreferences("lasthomev2advert", context.MODE_PRIVATE);
        return role.getString("lasthome2vAdvertTime", "0");
    }
}
